/**
 * 
 */
package net.mikoo.seals.agent.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.mikoo.seals.agent.biz.services.UpfileServices;
import net.mikoo.seals.agent.common.BaseController;
import net.mikoo.seals.agent.common.WebContext;
import net.mikoo.seals.agent.common.exception.ParametersException;
import net.mikoo.seals.agent.common.utils.IPUtil;
import net.mikoo.seals.agent.model.Party;
import net.mikoo.seals.agent.model.Upfile;

@Controller
@RequestMapping("/admin/upload")
public class UploadController extends BaseController {

	Logger logger = Logger.getLogger(UploadController.class);

	@Autowired
	private UpfileServices upfileServices;
	
	
	@RequestMapping("/testupload")
	public String testUpload() {
		
		
		return "/admin/upload/testupload";
	}
	
	

	@RequestMapping("/fileupload")
	@ResponseBody
	public Map<String, Object> fileupload(Upfile upfile, String module, HttpServletRequest request) {

		try {

			Party party = this.getParty(request, WebContext.ADMIN_LOGIN_SESSION_KEY);
			upfile.setPartyId(party.getPartyId());
			upfile.setUploadIp(IPUtil.getRemoteAddr(request));
			upfile.setUploadTime(new Date());
			upfile = upfileServices.upload(upfile);

			Map<String, Object> data = new HashMap<>();
			data.put("filename", upfile.getFileName());
			data.put("fileurl", "http://asset.51maijike.com/"+upfile.getFileUrl());
			data.put("filepath", upfile.getFileUrl());

			return result(SUCCESS, MSG_SUCCESS, data);

		} catch (ParametersException e) {
			return result(ERROR, e.getMessage());
		} catch (Exception e) {
			return result(ERROR, MSG_SYSTEM_ERROR);
		}

	}

}